package com.archetypus.projectschedule.web.involvements.cmd;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.archetypus.projectschedule.common.dto.involvements.Involvement;
import com.archetypus.projectschedule.common.dto.involvements.InvolvementKey;
import com.archetypus.projectschedule.common.dto.people.Person;
import com.archetypus.projectschedule.common.dto.people.PersonKey;
import com.archetypus.projectschedule.common.exceptions.ProjectSchedulerException;
import com.archetypus.projectschedule.web.common.cmd.ICommand;
import com.archetypus.projectschedule.web.involvements.bd.IInvolvementBD;
import com.archetypus.projectschedule.web.involvements.bd.InvolvementBD;

public class UpdateInvolvementCmd implements ICommand {

	public void execute(HttpServletRequest request,HttpServletResponse response) throws ProjectSchedulerException {
		int projectId = Integer.parseInt(request.getParameter("project"));
		int activityId = Integer.parseInt(request.getParameter("activity"));
		int newResponsible = Integer.parseInt(request.getParameter("responsibleId"));
		int oldResponsible = Integer.parseInt(request.getParameter("responsible"));
		String beginning = request.getParameter("beginning");
		String plannedEnd = request.getParameter("plannedEnd");
		
		InvolvementKey involvementKey = new InvolvementKey();
		involvementKey.setActivity(activityId);
		involvementKey.setProject(projectId);
		involvementKey.setResponsible(oldResponsible);
		
		Person responsible = new Person();
		responsible.setPersonKey(new PersonKey(newResponsible));
		
		Involvement involvement = new Involvement();
		involvement.setResponsible(responsible);
		involvement.setInvolvementKey(involvementKey);
		try{
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			involvement.setBeginning((Date)formatter.parse(beginning));
			involvement.setPlannedEnd((Date)formatter.parse(plannedEnd));
		}catch(Exception e){
			throw new ProjectSchedulerException("Error interpreting date",e);
		}
		
		IInvolvementBD involvementBD = new InvolvementBD();
		involvementBD.updateInvolvement(involvement);
		
	}

}
